package org.blogsite.greencode.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.blogsite.greencode.bean.TeachingSupport_Course;
import org.blogsite.greencode.bean.test.HibernateSessionFactory;
import org.blogsite.greencode.dao.TeachingSupport_CourseDao;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class TeachingSupport_CourseDaoImpl extends HibernateDaoSupport implements TeachingSupport_CourseDao {
	TeachingSupport_Course modifyCourse;
	//教学课程-保存
	public void save(TeachingSupport_Course course) {
		try {
			getHibernateTemplate().save(course);
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
	}
	//教学课程-查询
	public Iterator search(String keyWord){
		/*List list=new ArrayList();	//保存全部查询集合
		List mohu=new ArrayList();	//保存模糊查询集合
		Iterator course = null;		//保存所有查询结果 并return
		int count = 0;				//保存所有查询记录数
		String hqlcount="select count() from TeachingSupport_Course";*/
		try{
		/*	if(keyWord==null){
//		查询全部
				String hql="from TeachingSupport_Course c order by c.cno desc";
				//list=getHibernateTemplate().loadAll(TeachingSupport_Course.class);
				list = getHibernateTemplate().find(hql);
				course = list.iterator();
				count = ((Integer) getHibernateTemplate().iterate("select count() "+hql).next()).intValue();
				System.out.println("count:"+count);
			}else{
//		模糊查询
				String key="%"+keyWord+"%";
				System.out.println("keyii:"+key);				// like '"+ key +"' 或 like '%"+ keyWord +"%'
				String hqlMohu="from TeachingSupport_Course c where c.cno like '"+ key +"' or c.name like '"+ key +"' or c.credit like '"+ key +"' or c.period like '"+ key +"' or c.type like '"+ key +"' or c.describe like '"+ key +"' order by c.cno desc";
				mohu = getHibernateTemplate().find(hqlMohu);
				course = mohu.iterator();
			
				count = ((Integer) getHibernateTemplate().iterate(hqlcount).next()).intValue();
				System.out.println("count:"+count);
			
				int c = ((Integer)getHibernateTemplate().executeFind(
							new HibernateCallback() {
								public Object doInHibernate(Session s)
										throws HibernateException, SQLException {
									String q = "select count(*) from TeachingSupport_Course";
									Query query = s.createQuery(q);
									return query.uniqueResult();
								}
							});
				System.out.println("c:"+c);*/
				return null;
//			}
			} catch (Exception e) {
				System.out.print(e.getMessage());
			}
			return null;
		}
	//教学课程-修改
	public void modify(TeachingSupport_Course course){
		
		try {
			getHibernateTemplate().update(course);
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
	}
	//教学课程-删除
	public void delete(int cno){
		//取出对应cno的类对象即删除对象
		modifyCourse=(TeachingSupport_Course) getHibernateTemplate().load(TeachingSupport_Course.class, cno);
		try {
			//删除该对象
			getHibernateTemplate().delete(modifyCourse);
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
	}
	public List allSearch(){
		//	 TODO Auto-generated method stub
		List search=getHibernateTemplate().find("from TeachingSupport_Course");
		System.out.print(search);
		return search;
	}
	public List byIdSearch(int id){
//		 TODO Auto-generated method stub
		return getHibernateTemplate().find("from TeachingSupport_Course tsc where tsc.id=?",id);
	}
	
	public List byNameSearch(String name) {
		// TODO Auto-generated method stub
		return getHibernateTemplate().find("from TeachingSupport_Course tsc where tsc.name=?",name);
	}
	public List byBlearSearch(String text) {
		return getHibernateTemplate().find("from TeachingSupport_Course tsc where tsc.name like ? or tsc.type like ? or tsc.describe like ?",new String[]{"%"+text+"%","%"+text+"%","%"+text+"%"});
	}
	/*查询指定名称的课程是否存在*/
	public boolean searchCourseByName(String name){
		try {
			
			Iterator iterator= getHibernateTemplate().find("from TeachingSupport_Course tsc where tsc.name=?",name).iterator();
			if(iterator.hasNext()){
				return true;
			}else
				return false;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
	}
}
